package com.itany.book.controller;

import com.github.pagehelper.PageInfo;
import com.itany.book.exception.ManagerUserNotExistsException;
import com.itany.book.pojo.ManagerUser;
import com.itany.book.service.ManagerUserService;
import com.itany.book.vo.ResponseResult;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * Author:yaozhiyuan0117@163.com
 * Time:2019/12/24 17:38
 * Describe:
 * version:1.0
 */
@Controller
@RequestMapping("/managerUser")
public class ManagerUserController {
    @Autowired
    private ManagerUserService managerUserService;

    @GetMapping("/login")
    public String login(ManagerUser user, HttpSession session){
        try {
            ManagerUser u = managerUserService.login(user);
            session.setAttribute("id",u.getId());
            session.setAttribute("supplierId",u.getSupplierid());
            System.out.println(session.getAttribute("id"));
            return "index";
        } catch (ManagerUserNotExistsException e) {
            return "login";
        }
    }

    @RequestMapping("/modifyPassword")
    @ResponseBody
    public ResponseResult modifyPassword(Integer id, String password){
        System.out.println("-------");
        System.out.println(id);
        System.out.println(password);

        ManagerUser user = new ManagerUser();
        user.setId(id);
        user.setPassword(password);
        managerUserService.modifyPassword(user);
        return ResponseResult.success();

    }


    @RequestMapping("/findAll")
    @ResponseBody
    public Map findAll(Integer page, Integer rows, String username){
        Map map = new HashMap();
        PageInfo<ManagerUser> info = managerUserService.findAll( page, rows, username);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    @RequestMapping("/addUser")
    @ResponseBody
    public ResponseResult addUser(String username, String password, @RequestParam("roles[]") Integer[] roles){
        managerUserService.addUser(username, password,roles);
        return ResponseResult.success();
    }

    @RequestMapping("/findById")
    @ResponseBody
    public ResponseResult findById(Integer id){
        ManagerUser user = managerUserService.findById(id);
        return ResponseResult.success(user);
    }

    @RequestMapping("/modifyUSer")
    @ResponseBody
    public ResponseResult modifyUSer(Integer id,
                                     String username,
                                     String password,
                                     @RequestParam("roles[]") Integer[] roles){

        System.out.println("-------------");
        System.out.println(id);
        System.out.println(username);
        System.out.println(roles);
        managerUserService.modifyUser(id,username,password,roles);
        return ResponseResult.success();
    }
}
